﻿using log4net;
using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using UIWindow.Domains;
using UIWindow.Enums;
using UIWindow.GlobalParameters;
using UIWindow.ViewModels.Dialogs;
using UIWindow.Views.Dialogs;

namespace UIWindow.Helper
{
    public static class LogMessage
    {
        //private static log4net.ILog message = log4net.LogManager.GetLogger("message");
        //private static log4net.ILog mes = log4net.LogManager.GetLogger("mes");
        private static readonly HomePageGlobal _pageGlobal;

        static LogMessage()
        {
            _pageGlobal = Bootstrapper.GetContainer().ResolveSingleton<HomePageGlobal>();
            _error = new SolidColorBrush(Color.FromArgb(0xFF, 0xF3, 0x51, 0x51));
            _warning = new SolidColorBrush(Color.FromArgb(0xFF, 0xF3, 0xCE, 0x51));
            _messError = new SolidColorBrush(Color.FromArgb(0xFF, 0xF3, 0x51, 0x51));
            _messWarn = new SolidColorBrush(Color.FromArgb(0xFF, 0xF3, 0xCE, 0x51));
            _messInfo = new SolidColorBrush(Color.FromArgb(0xF9, 0x08, 0xB9, 0x89));
        }

        private static void MyLog(string str, SolidColorBrush brush)
        {
            if (_pageGlobal.LogList.Count > 400) _pageGlobal.LogList.RemoveAt(399);
            _pageGlobal.LogList.Insert(0, new Models.RunMessage()
            {
                Message = str,
                MessageForeground = brush,
                RunTime = DateTime.Now.ToString("T")
            });
        }
        private static SolidColorBrush _error;
        private static SolidColorBrush _warning;
        /// <summary>
        /// 运行日志
        /// </summary>
        /// <param name="str"></param>
        /// <param name="levelType"></param>
        /// <param name="name">message为运行日志 mes为MES访问日志</param>
        public static void RunLog(this string str, MessageLevelType levelType, string name = "message")
        {
            ILog log = LogManager.GetLogger(name);
            switch (levelType)
            {
                case MessageLevelType.Error:
                    MyLog(str, _error);
                    log.Error($"{str}\r\n");
                    break;
                case MessageLevelType.Info:
                    MyLog(str, Brushes.Black);
                    log.Info($"{str}\r\n");
                    break;
                case MessageLevelType.Warning:
                    MyLog(str, _warning);
                    log.Warn($"{str}\r\n");
                    break;
            }
        }

        /// <summary>
        /// UI线程弹窗
        /// </summary>
        /// <param name="str"></param>
        /// <param name="levelType"></param>
        public static void MessageDialogs(this string str, MessageLevelType levelType)
        {
            if (DialogHost.IsDialogOpen("RootDialog")) DialogHost.Close("RootDialog");
            string title = null;
            PackIconKind pack = PackIconKind.AllInclusive;
            Brush brush = null;
            switch (levelType)
            {
                case MessageLevelType.Error:
                    brush = new SolidColorBrush(Color.FromArgb(0xFF, 0xF3, 0x51, 0x51));
                    pack = PackIconKind.Alert;
                    title = "操 作 失 败";
                    break;
                case MessageLevelType.Info:
                    brush = new SolidColorBrush(Color.FromArgb(0xF9, 0x08, 0xB9, 0x89));
                    pack = PackIconKind.AlertCircle;
                    title = "操 作 成 功";
                    break;
                case MessageLevelType.Warning:
                    brush = new SolidColorBrush(Color.FromArgb(0xFF, 0xF3, 0xCE, 0x51));
                    pack = PackIconKind.AlertCircle;
                    title = "操 作 警 告";
                    break;
            }
            DialogHost.Show(new MessageDialogs()
            {
                DataContext = new MessageDialogsViewModel(title, str, pack, brush)
            }, "RootDialog").ConfigureAwait(false);
        }
        private static Brush _messError;
        private static Brush _messInfo;
        private static Brush _messWarn;
        /// <summary>
        /// 跨UI线程弹窗
        /// </summary>
        /// <param name="str"></param>
        /// <param name="levelType"></param>
        public static void MessageDialogsUI(this string str, MessageLevelType levelType)
        {
            Bootstrapper.UIAsync(e =>
            {
                if (DialogHost.IsDialogOpen("RootDialog")) DialogHost.Close("RootDialog");
                ILog log = LogManager.GetLogger("mes");
                string title = null;
                PackIconKind pack = PackIconKind.AllInclusive;
                Brush brush = null;
                switch (levelType)
                {
                    case MessageLevelType.Error:
                        brush = _messError;
                        pack = PackIconKind.Alert;
                        title = "操 作 失 败";
                        log.Error($"{str}\r\n");
                        break;
                    case MessageLevelType.Info:
                        brush = _messInfo;
                        pack = PackIconKind.AlertCircle;
                        title = "操 作 成 功";
                        log.Info($"{str}\r\n");
                        break;
                    case MessageLevelType.Warning:
                        brush = _messWarn;
                        pack = PackIconKind.AlertCircle;
                        title = "操 作 警 告";
                        log.Info($"{str}\r\n");
                        break;
                }
                DialogHost.Show(new MessageDialogs()
                {
                    DataContext = new MessageDialogsViewModel(title, str, pack, brush)
                }, "RootDialog").ConfigureAwait(false);
            }, null);
        }
    }
}
